package zzs.generalframework.console.core.parameter.technicalparameter.sysconfig.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import zzs.generalframework.common.core.parameter.technicalparameter.sysconfig.SysConfigBO;
import zzs.generalframework.common.core.parameter.technicalparameter.sysconfig.SysConfigInfo;
import zzs.generalframework.common.core.parameter.technicalparameter.sysconfig.SysConfigPO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.parameter.technicalparameter.sysconfig.dao.ISysConfigDao;
import zzs.generalframework.console.core.parameter.technicalparameter.sysconfig.service.ISysConfigService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.technicalparameter.sysconfig.service.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
public class SysConfigServiceImpl implements ISysConfigService
{

	private Logger logger = LoggerFactory.getLogger(SysConfigServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	ISysConfigDao sysConfigDao;

	@Override
	public ConsoleReturnBO saveSysConfig(SysConfigBO sysConfigBO)
	{
		logger.debug("====保存系统配置信息开始====");
		logger.debug("====保存系统配置信息.入参:SysConfigBO=[{}]====", sysConfigBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		SysConfigPO sysConfigPO = new SysConfigPO();
		copySysConfigBO2SysConfigPO(sysConfigBO, sysConfigPO);
		Boolean insertResult = sysConfigDao.insert(sysConfigPO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存系统配置信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存系统配置信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveSysConfig(List<SysConfigBO> sysConfigBOList) throws ConsoleException
	{
		logger.info("====保存系统配置信息列表开始====");
		logger.debug("====保存系统配置信息列表.入参:SysConfigBOList=[{}]====", sysConfigBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (SysConfigBO itemBO : sysConfigBOList)
		{
			ConsoleReturnBO itemResult = saveSysConfig(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存系统配置信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存系统配置信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getSysConfig(SysConfigInfo sysConfigInfo)
	{
		logger.info("====获取系统配置信息开始====");
		logger.debug("====获取系统配置信息.入参:SysConfigInfo=[{}]====", sysConfigInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<SysConfigPO> sysConfigPOList = sysConfigDao.select(sysConfigInfo);
		if (sysConfigPOList == null || sysConfigPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (sysConfigPOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			SysConfigBO sysConfigBO = new SysConfigBO();
			copySysConfigPO2SysConfigBO(sysConfigPOList.get(0), sysConfigBO);
			returnConsoleReturnBO.setReturnObj(sysConfigBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			SysConfigBO sysConfigBO = new SysConfigBO();
			copySysConfigPO2SysConfigBO(sysConfigPOList.get(0), sysConfigBO);
			returnConsoleReturnBO.setReturnObj(sysConfigBO);
		}
		logger.debug("====获取系统配置信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取系统配置信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listSysConfig(SysConfigInfo sysConfigInfo)
	{
		logger.info("====获取系统配置信息列表开始====");
		logger.debug("====获取系统配置信息列表.入参:SysConfigInfo=[{}]====", sysConfigInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<SysConfigPO> sysConfigPOList = sysConfigDao.select(sysConfigInfo);
		if (sysConfigPOList == null || sysConfigPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<SysConfigBO> resultList = new ArrayList<SysConfigBO>();
			for (SysConfigPO itemPO : sysConfigPOList)
			{
				SysConfigBO itemBO = new SysConfigBO();
				copySysConfigPO2SysConfigBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取系统配置信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取系统配置信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateSysConfig(SysConfigBO sysConfigBO)
	{
		logger.info("====更新系统配置信息开始====");
		logger.debug("====更新系统配置信息.入参:SysConfigBO=[{}]====", sysConfigBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		SysConfigPO sysConfigPO = new SysConfigPO();
		copySysConfigBO2SysConfigPO(sysConfigBO, sysConfigPO);
		Boolean updateResult = sysConfigDao.update(sysConfigPO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新系统配置信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新系统配置信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateSysConfig(List<SysConfigBO> sysConfigBOList) throws ConsoleException
	{
		logger.info("====更新系统配置信息列表开始====");
		logger.debug("====更新系统配置信息列表.入参:List<SysConfigBO>=[{}]====", sysConfigBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (SysConfigBO itemBO : sysConfigBOList)
		{
			ConsoleReturnBO itemResult = updateSysConfig(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====更新系统配置信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新系统配置信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeSysConfig(SysConfigBO sysConfigBO)
	{
		logger.info("====删除系统配置信息开始====");
		logger.debug("====删除系统配置信息.入参:SysConfigBO＝[{}]======", sysConfigBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		SysConfigPO sysConfigPO = new SysConfigPO();
		copySysConfigBO2SysConfigPO(sysConfigBO, sysConfigPO);
		Boolean deleteResult = sysConfigDao.delete(sysConfigPO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除系统配置信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除系统配置信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeSysConfig(List<SysConfigBO> sysConfigBOList) throws ConsoleException
	{
		logger.info("====删除系统配置信息列表开始====");
		logger.debug("====删除系统配置信息列表.入参:List<SysConfigBO>＝[{}]======", sysConfigBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (SysConfigBO itemBO : sysConfigBOList)
		{
			ConsoleReturnBO itemResult = removeSysConfig(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除系统配置信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除系统配置信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countSysConfig(SysConfigBO sysConfigBO)
	{
		logger.info("====统计系统配置信息开始====");
		logger.debug("====统计系统配置信息。入参：SysConfigBO＝[{}]======", sysConfigBO);
		Long returnLong = new Long(0);
		SysConfigPO sysConfigPO = new SysConfigPO();
		copySysConfigBO2SysConfigPO(sysConfigBO, sysConfigPO);
		returnLong = sysConfigDao.count(sysConfigPO);
		logger.debug("====统计系统配置信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计系统配置信息结束====");
		return returnLong;
	}

	private void copySysConfigBO2SysConfigPO(SysConfigBO sysConfigBO, SysConfigPO sysConfigPO)
	{
		beanUtils.copyBeanNotNull(sysConfigBO, sysConfigPO);
		if (sysConfigBO.getSysID() != null)
		{
			sysConfigPO.setSeqID(sysConfigBO.getSysID());
		}
	}

	private void copySysConfigPO2SysConfigBO(SysConfigPO sysConfigPO, SysConfigBO sysConfigBO)
	{
		beanUtils.copyBeanNotNull(sysConfigPO, sysConfigBO);
		if (sysConfigPO.getSeqID() != null)
		{
			sysConfigBO.setSysID(sysConfigPO.getSeqID());
		}
	}

}
